package com.devframe.sysconfig.storage.strategy;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;


import com.devframe.sysconfig.storage.SysConfigDataStorage;


public class LocalSysConfigDataStorage extends SysConfigDataStorage {
	private static Logger logger = Logger.getLogger(LocalSysConfigDataStorage.class);
    /**
     * codeTables 代码表缓存
     */
    private Map<String,Map> codeTables = new ConcurrentHashMap<String,Map>();

    /**
     * codeTableSet 代码表集合
     */
    private Map<String,Map> codeTableSet = new ConcurrentHashMap<String,Map>();
    
	/**
	 * sysParams 系统参数缓存
	 */
	private Map<String,String> sysParams = new ConcurrentHashMap<String,String>();
	
	/**
	 * ctrlTables
	 * 控制表缓存
	 */
	private Map<String,List> ctrlTables=new ConcurrentHashMap<String,List>();
	/**
	 * ctrlTableSet
	 * 配置的控制表，在初始化时加载
	 */
	private Map<String,String> ctrlTableSet = new ConcurrentHashMap<String,String>();
	
    
	public void clearCodeTableData(){
		this.codeTableSet.clear();
		this.codeTables.clear();
	}
	
	public void addCodeTableSet(String key, Map val) {
		if(val==null){
			logger.info("加载系统参数:key="+key+",值为空："+val);
		}
		//加入同步判断
		this.codeTableSet.put(key, val);
	}
	public   boolean containsKeyInCodeTableSet(String key){
		return this.codeTableSet.containsKey(key);
	}
	public  boolean containsKeyInCodeTable(String key){
		return this.codeTables.containsKey(key);
	}
	public  Map getCodeTableSet(String key){
		return this.codeTableSet.get(key);
	}

	public Map getCodeTable(String codeTableName) {
		return this.codeTables.get(codeTableName.toUpperCase());
	}
	public void addCodeTable(String tableName,Map val){
		this.codeTables.put(tableName.toUpperCase(), val);
	}
///////////////////////////////系统参数////////////////////////////////////////////
	public void clearSysParamData() {
		this.sysParams.clear();
	}

	public boolean containsKeyInSysParam(String key) {
		return this.sysParams.containsKey(key);
	}

	public String getSysParam(String key) {
		return this.sysParams.get(key);
	}

	public String addSysParam(String key, String value) {
		if(StringUtils.isEmpty(value)){
			logger.info("加载系统参数:key="+key+",值为空："+value);
		}
		return this.sysParams.put(key, value);
	}
	
	public void updateSysParam(String key, String value){
		this.sysParams.put(key, value);
	}

	public void setSysParam(String key, String value) {
		this.sysParams.put(key, value);
	}
	
	
	public void clearCtrlTableData(){
		this.ctrlTableSet.clear();
		this.codeTables.clear();
	}

	public boolean containsKeyInCtrlTableSet(String ctrlTableName) {
		return this.ctrlTableSet.containsKey(ctrlTableName);
	}

	public boolean containsKeyInCtrlTable(String ctrlTableName) {
		return this.ctrlTables.containsKey(ctrlTableName);
	}
	public  void addCtrlTableSet(String tableAlias,String tableName){
		if(StringUtils.isEmpty(tableName)){
			logger.info("加载系统参数:key="+tableAlias+",值为空："+tableName);
		}
		this.ctrlTableSet.put(tableAlias, tableName);
	}

	public void addCtrlTable(String tableName, List tableList) {
		if(tableList==null){
			logger.info("加载系统参数:key="+tableName+",值为空："+tableList);
		}
		this.ctrlTables.put(tableName, tableList);
	}

	public String getCtrlTableSet(String tableAlias) {
		return this.ctrlTableSet.get(tableAlias);
	}

	public List getCtrlTable(String tableAlias) {
		return this.ctrlTables.get(tableAlias);
	}

	public void removeCtrlTable(String tableAlias) {
		this.codeTables.remove(tableAlias);
	}
	
	
	
}
